package com.vogue.supplymall.shop.domain.service.impl;

import com.vogue.supplymall.common.domain.dto.QueryListResultInfo;
import com.vogue.supplymall.common.domain.dto.RelevanceInfo;
import com.vogue.supplymall.common.domain.dto.SearchInfo;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;
import com.vogue.supplymall.common.exception.DataNotFindException;
import com.vogue.supplymall.shop.common.CodeTransformUtils;
import com.vogue.supplymall.shop.common.IMemUtils;
import com.vogue.supplymall.shop.domain.dao.BaseDao;
import com.vogue.supplymall.shop.domain.dao.OrganShopImgDao;
import com.vogue.supplymall.shop.domain.dao.RelevanceDao;
import com.vogue.supplymall.shop.domain.dao.ShopDao;
import com.vogue.supplymall.shop.domain.model.OrganShopImg;
import com.vogue.supplymall.shop.domain.model.Relevance;
import com.vogue.supplymall.shop.domain.model.Shop;
import com.vogue.supplymall.shop.domain.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Shop service.
 *
 * @aouthor: zhangliang
 * @create: 2017 -12-01
 */
@Service("shopService")
public class ShopServiceImpl extends BaseServiceImpl<Shop, Long> implements ShopService {

    @Resource
    private ShopDao shopDao;
    @Resource
    private OrganShopImgDao organShopImgDao;
    @Resource
    private IMemUtils iMemUtils;
    @Resource
    private CodeTransformUtils codeTransformUtils;
    @Autowired
    private MessageSourceUtils messageSourceUtils;
    @Resource
    private RelevanceDao relevanceDao;

    /**
     * Instantiates a new Shop service.
     *
     * @param baseDao the base dao
     */
    public ShopServiceImpl(BaseDao<Shop, Long> baseDao) {
        super(baseDao);
//        shopDao = (ShopDao) baseDao;
    }

    @Override
    public Map<String, Object> findById(Long id, Long loginId) {
        if (null == id) {
            throw new DataNotFindException(
                    messageSourceUtils.getMessage(
                            com.vogue.supplymall.common.constant.ConstantBean.GLOBAL_SYSTEM
                                    + "product", null));
        }

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        Shop shop = this.selectOneByWhr0("disabledflag = 0 AND hiddenflag = 0 AND id = #{id}", map);
        if (null != shop) {
        	//如果没有得到门店的图片，那么默认店铺图片为企业图片
        	List<OrganShopImg> shopImgList = organShopImgDao.findBySysCode(shop.getSyscodeid(), shop.getOrganid());
        	if (shopImgList == null || shopImgList.size() == 0) {
        		shopImgList = organShopImgDao.findEntImgBySysCode(shop.getSyscodeid());
        	}
            shop.setOrganShopImg(shopImgList);

            List<Relevance> relevances = new ArrayList<>();
            if (null != loginId) {
                /* 2018/3/30 商品关注修改*/
                RelevanceInfo relevance = new RelevanceInfo();
                relevance.setTargetObjId(id);
                relevance.setType(1);
                relevance.setUserId(loginId);
                relevance.setTargetObjType(3);
                relevances = relevanceDao.findRelevance(relevance);
            }
            result.put("shop", shop);
            if (relevances.size() > 0) {
                result.put("isWatch", "1");
                result.put("watchId", relevances.get(0).getId());
            } else {
                result.put("isWatch", "0");
                result.put("watchId", 0L);
            }
        }

        return result;
    }

    @Override
    public List<Map<String, Object>> findBySearchInfo(SearchInfo searchInfo) {
        if (null == searchInfo) {
            return new ArrayList<>();
        }
        return codeTransformUtils.searchArea(shopDao.findShop(searchInfo));
    }

    @Override
    @Transactional
    public List<Shop> addShop(List<Shop> shops) {
        if (null != shops) {
            for (Shop shop : shops) {
                if (null != shop) {
                    shopDao.save(shop);
                }
            }
            return shops;
        }
        return new ArrayList<>();
    }

    @Override
    public Shop getShop(Long sysCodeId, Long organId) {
        if (null != sysCodeId && null != organId) {
            List<Shop> result = shopDao.findShopForSql(sysCodeId, organId);
            if (result.size() > 0) {
                return result.get(0);
            }
        }
        return null;
    }

    @Override
    public Shop findShopByMS(Long syscodeId, String matchId,int tendId) {
        if (null != syscodeId && null != matchId) {
            List<Shop> result = shopDao.findShopByMS(syscodeId, matchId,tendId);
            if (result.size() > 0) {
                return result.get(0);
            }
        }
        return null;
    }

    @Override
    public void changeHiddenFlagOfShop(Long shopId, Long sysCodeId, int hiddenFlag) {
        shopDao.changeHiddenFlagOfShop(shopId, sysCodeId, hiddenFlag);

    }

    @Override
    public List<Map<String, Object>> findLicenceShopBySql(SearchInfo searchInfo) throws RuntimeException {
        return shopDao.findLicenceShopBySql(searchInfo);
    }

    @Override
    public Long countValidShop() {
        return shopDao.countValidShop();
    }

    @Override
    public Long countBySyscodeid(Long syscodeid) {
        String sql = "select COUNT(DISTINCT matchid) count FROM shop where syscodeid = #{syscodeid} ";
        Map<String, Long> para = new HashMap<>();
        para.put("syscodeid", syscodeid);
        List<Map<String, Object>> result = shopDao.query0(sql, para);
        return result.size() > 0 ? new Long(result.get(0).get("count").toString()) : 0L;
    }

    @Override
    public void updateEntnameBySyscodeId(Long syscodeId, String entName) throws Exception {
        String sql = "update shop set entname='" + entName + "' where syscodeid=" + syscodeId;
        this.update(sql);
    }

	@Override
	public List<Map<String, Object>> findShopCategoryIds(Long id) {
		// TODO Auto-generated method stub
		return shopDao.query("select distinct rpc.categoryid id from product p,"
                + " rel_product_category rpc where "
                + "p.id = rpc.productid and shopid=" + id);
    }

    @Override
    public void updateWaitingOrderBySyscodeId(Long syscodeId, Boolean waitingOrder) throws Exception {
        int waitingOrderFlag = 0;
        if (waitingOrder) {
            waitingOrderFlag = 1;
        }
        shopDao.updateWaitingOrderBySyscodeId(syscodeId, waitingOrderFlag);

    }

	@Override
	public QueryListResultInfo findListBySearchInfo(SearchInfo searchInfo) {
		QueryListResultInfo result = new QueryListResultInfo();
		result.setRecordList(findBySearchInfo(searchInfo));
		result.setRecordCount(shopDao.findShopCount(searchInfo));
		return result;
	}

}
